package com.example.empleemanagesystem.controller;

import com.example.empleemanagesystem.entity.Attendance;
import com.example.empleemanagesystem.entity.Employee;
import com.example.empleemanagesystem.service.AttendanceService;
import com.example.empleemanagesystem.service.IEmployeeService;
import com.example.empleemanagesystem.service.SalaryManageService;
import com.example.empleemanagesystem.vo.AttendanceVO;
import com.example.empleemanagesystem.vo.SalaryManageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * 该控制器处理与员工操作相关的HTTP请求。
 * 它使用IEmployeeService来执行实际的业务逻辑。
 *
 * @since 2024-11-30
 */

// Spring RESTful风格的Web服务控制器，处理HTTP请求并返回JSON数据
@RestController
@RequestMapping("/employee") // 基础请求路径
public class EmployeeController {
    @Autowired // 自动注入IEmployeeService实例
    private IEmployeeService employeeService;

    @Autowired // 自动注入SalaryManageService实例
    private SalaryManageService salaryManageService;

    @Autowired // 自动注入AttendanceService实例
    private AttendanceService attendanceService;

    /**
     * 保存或更新员工记录
     *
     * @param employee 要保存或更新的员工对象
     * @return 如果操作成功返回true，否则返回false
     */
    @PostMapping("/saveOrUpdate")
    public boolean saveOrUpdate(@RequestBody Employee employee) {
        // 调用employeeService的saveOrUpdate方法保存或更新员工记录
        boolean result = employeeService.saveOrUpdate(employee);
        if (result) {
            // 如果保存或更新成功，创建并保存对应的SalaryManageVO和Attendance记录
            SalaryManageVO salaryManageVO = new SalaryManageVO();
            salaryManageVO.setNumber(employee.getNumber().toString());
            salaryManageVO.setName(employee.getName());
            salaryManageVO.setBaseSalary(employee.getSalary().doubleValue());
            salaryManageVO.setBonus(0.0);
            salaryManageVO.setDeduction(0.0);
            salaryManageService.create(salaryManageVO);

            Attendance attendance = new Attendance();
            attendance.setNumber(employee.getNumber().toString());
            attendance.setName(employee.getName());
            attendance.setArrive_late_times(0);
            attendance.setLeave_early_times(0);
            attendanceService.create(new AttendanceVO(attendance.getId(), attendance.getNumber(), attendance.getName(), attendance.getArrive_late_times(), attendance.getLeave_early_times()));
        }
        return result;
    }

    /**
     * 获取所有员工列表
     *
     * @return 员工对象列表
     */
    @GetMapping("/list")
    public List<Employee> show(){
        // 调用employeeService的list方法获取所有员工记录
        return employeeService.list();
    }

    /**
     * 根据员工编号删除员工记录
     *
     * @param number 要删除的员工的编号
     * @return 如果操作成功返回true，否则返回false
     */
    @DeleteMapping("/delete/{number}")
    public boolean deleteEmployeeByNumber(@PathVariable String number) {
        // 调用employeeService的removeByNumber方法删除员工记录
        boolean result = employeeService.removeByNumber(number);
        if (result) {
            // 如果删除成功，删除对应的SalaryManage和Attendance记录
            salaryManageService.getByNumber(number).ifPresent(salaryManage -> {
                salaryManageService.deleteById(salaryManage.getId());
            });

            attendanceService.getByNumber(number).ifPresent(attendance -> {
                attendanceService.deleteById(attendance.getId());
            });
        }
        return result;
    }

    /**
     * 更新员工的部门
     *
     * @param id 要更新的员工的ID
     * @param department 要设置的新部门
     * @return 如果操作成功返回true，否则返回false
     */
    @PutMapping("/updateDepartment/{id}")
    public boolean updateDepartment(@PathVariable Integer id, @RequestParam String department) {
        // 根据ID获取员工记录
        Employee employee = employeeService.getById(id);
        if (employee != null) {
            // 设置新的部门并更新记录
            employee.setDepartment(department);
            return employeeService.updateById(employee);
        }
        return false;
    }

    /**
     * 更新员工的生日
     *
     * @param id 要更新的员工的ID
     * @param birthday 要设置的新生日
     * @return 如果操作成功返回true，否则返回false
     */
    @PutMapping("/updateBirthday/{id}")
    public boolean updateBirthday(@PathVariable Integer id, @RequestParam String birthday) {
        // 根据ID获取员工记录
        Employee employee = employeeService.getById(id);
        if (employee != null) {
            // 设置新的生日并更新记录
            employee.setBirthday(birthday);
            return employeeService.updateById(employee);
        }
        return false;
    }

    /**
     * 更新员工的薪水
     *
     * @param id 要更新的员工的ID
     * @param salary 要设置的新薪水
     * @return 如果操作成功返回true，否则返回false
     */
    @PutMapping("/updateSalary/{id}")
    public boolean updateSalary(@PathVariable Integer id, @RequestParam Integer salary) {
        // 根据ID获取员工记录
        Employee employee = employeeService.getById(id);
        if (employee != null) {
            // 设置新的薪水并更新记录
            employee.setSalary(salary);
            boolean result = employeeService.updateById(employee);
            if (result) {
                // 如果更新成功，更新对应的SalaryManage记录
                salaryManageService.getByNumber(employee.getNumber().toString()).ifPresent(salaryManage -> {
                    salaryManage.setBaseSalary(salary.doubleValue());
                    salaryManageService.update(salaryManage);
                });
            }
            return result;
        }
        return false;
    }

    /**
     * 更新员工的职位
     *
     * @param id 要更新的员工的ID
     * @param position 要设置的新职位
     * @return 如果操作成功返回true，否则返回false
     */
    @PutMapping("/updatePosition/{id}")
    public boolean updatePosition(@PathVariable Integer id, @RequestParam String position) {
        // 根据ID获取员工记录
        Employee employee = employeeService.getById(id);
        if (employee != null) {
            // 设置新的职位并更新记录
            employee.setPosition(position);
            return employeeService.updateById(employee);
        }
        return false;
    }
}